package cn.biplam.front.component.data.tree.treebase;

import cn.biplam.common.bean.tree.TreeParentData;
import cn.biplam.common.bean.tree.TreeParentDataMap;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.front.component.data.tree.TreeChainData;
import cn.biplam.front.component.data.tree.TreeChainDataMap;
import cn.biplam.front.component.data.tree.TreeData;
import cn.biplam.front.component.data.tree.treebase.Tree_Type.Tree_DC_Type;
import cn.biplam.front.html.base.HtmlWindow;

/**
 * 父子树
 * @author WangSong 2009-10-31下午12:28:25
 * @version 1.0
 */
public class TreeDcParent  extends TreeDC {
	
	/** 虚根名称*/
	private String VirtualRootName;
	/** 展开的节点id*/
	private String nodeId="-1";
	/** 展开的节点级别*/
	private String nodeLevel;
	/** 节点向下展开几级*/
	private String levelDown;
	/** 节点是否是否有哥哥*/
	private String haveBrother;
	/** 节点是否是否有弟弟*/
	private String haveLBrother;
	/** 父亲是否有弟弟*/
	private String parentsHaveLBrother;
	/**与树的标准字段的映射 */
	private TreeChainDataMap fieldMap;
	/** 父子表的表名*/
	private String tableName;
	
	/**
	 * 无参构造,ajx调用时使用
	 */
	public TreeDcParent(){
		
	}
	
	/**
	 * 构造函数（默认levelDown为1,默认父子树的根节点为-1）
	 * @param mUser 用户信息
	 * @param tableName 索引表名
	 * @param VirtualRootName 虚根名称
	 * @throws ErrorMessage 
	 */
	public TreeDcParent(UserInfo mUser,
	           		    String tableName,
	           		    String VirtualRootName) throws ErrorMessage{
		/**默认levelDown为1,默认父子树的根节点为-1*/
		this(mUser,tableName,VirtualRootName,"1","-1");
	}
	
	/**
	 * 构造函数(自定义树默认展开级别和根节点id)
	 * @param mUser 用户信息
	 * @param tableName 索引表名
	 * @param VirtualRootName 虚根名称
	 * @param levelDown 向下展开级别
	 * @throws ErrorMessage 
	 */
	public TreeDcParent(UserInfo mUser,
	           		    String tableName,
	           		    String VirtualRootName,
	           		    String levelDown,
	           		    String nodeId) throws ErrorMessage {
		//根节点时nodeLevel默认为0
		this(mUser,tableName,VirtualRootName,levelDown,nodeId,"0",null,null,null);
	}
	
	/**
	 * 构造方法(树展开时模版内部调用此构造方法)
	 * @param mUser    用户信息
	 * @param sHomeUrl 系统路径
	 * @param tableName 索引表名
	 * @param nodeType  树的节点类型
	 * @param VirtualRootName 虚根名称
	 * @param mTree 树模版对象
	 * @param pageId 顶级页面id(持久化树用)
	 * @param pcid 调用树的vc的容器的id
	 * @param aId 树Vc的行为id
	 * @throws ErrorMessage 
	 */
	public TreeDcParent(UserInfo mUser,
			           String tableName,
			           String VirtualRootName,
			           String levelDown,
			           String nodeId,
			           String nodeLevel,
			           String haveBrother,
			           String haveLBrother,
			           String parentsHaveLBrother) throws ErrorMessage{
		TOOLS.checkNull(mUser, "TreeDcIndex init error,mUser");
		TOOLS.checkNull(tableName, "TreeDcIndex init error,tableName");
		TOOLS.checkNull(nodeId, "TreeDcIndex init error,nodeId");
		TOOLS.checkNull(nodeLevel, "TreeDcIndex init error,nodeLevel");
		TOOLS.checkNull(levelDown, "TreeDcIndex init error,levelDown");
		this.mUser = mUser;
		this.VirtualRootName = VirtualRootName;
		this.nodeId = nodeId;
		this.nodeLevel = nodeLevel;
		this.levelDown = levelDown;
		this.haveBrother = haveBrother;
		this.haveLBrother = haveLBrother;
		this.parentsHaveLBrother = parentsHaveLBrother;
		this.tableName = tableName;
		fieldMap = new TreeChainDataMap();
		fieldMap.setNodeType("node_type");
		fieldMap.setOperatorType("d_operator_type");
		fieldMap.setNodeLevel("node_level");
	}
	
	/**
	 * 设置字段映射-节点id(必须设置)
	 * @param nodeId 节点id字段名
	 */
	public void setMappingNodeId(String nodeId){
		fieldMap.setNodeId(nodeId);
	}
	
	/**
	 * 设置字段映射-节点名称(必须设置)
	 * @param nodeName 节点名称字段名
	 */
	public void setMappingNodeName(String nodeName){
		fieldMap.setNodeName(nodeName);
	}
	
	/**
	 * 设置字段映射-节点操作类型(可不设置,默认都为1)
	 * @param nodeType 节点操作类型字段名
	 */
	public void setMappingOperatorType(String nodeType){
		fieldMap.setOperatorType(nodeType);
	}
	
	/**
	 * 设置字段映射-真实id(必须设置)
	 * @param realId 真实id字段名
	 */
	public void setMappingRealId(String realId){
		fieldMap.setRealId(realId);
	}
	
	/**
	 * 设置字段映射-父id(必须设置)
	 * @param nodeIdName 父id字段名
	 */
	public void setMappingNodeParent(String nodeParent){
		fieldMap.setNodeParent(nodeParent);
	}
	
	/**
	 *  设置父亲们是否有弟弟
	 * @param parentsHaveLBrother
	 */
	public void setParentsHaveLBrother(String parentsHaveLBrother){
		this.parentsHaveLBrother = parentsHaveLBrother;
	}
	
	/**
	 *  设置是否有弟弟
	 * @param haveLBrother
	 */
	public void setHaveLBrother(String haveLBrother){
		this.haveLBrother = haveLBrother;
	}
	
	/**
	 *  设置是否有哥哥
	 * @param haveBrother
	 */
	public void setHaveBrother(String haveBrother){
		this.haveBrother = haveBrother;
	}
	
	/**
	 *  设置向下级别
	 * @param levelDown
	 */
	public void setLevelDown(String levelDown){
		this.levelDown = levelDown;
	}
	
	/**
	 *  设置节点级别
	 * @param nodeLevel
	 */
	public void setNodeLevel(String nodeLevel){
		this.nodeLevel = nodeLevel;
	}
	
	/**
	 *  设置节点ID
	 * @param nodeId
	 */
	public void setNodeId(String nodeId){
		this.nodeId = nodeId;
	}
	
	@Override
	public TreeData get() throws ErrorMessage {
		/**检查字段映射*/
		TOOLS.checkNull(this.fieldMap.getNodeId(),"TreeDcParent Error,not set fieldMap ,nodeId ");
		TOOLS.checkNull(this.fieldMap.getNodeName(),"TreeDcParent Error,not set fieldMap,nodeName ");
		TOOLS.checkNull(this.fieldMap.getNodeParent(),"TreeDcParent Error,not set fieldMap,nodeparent ");
		TOOLS.checkNull(this.fieldMap.getRealId(),"TreeDcParent Error,not set fieldMap,nodeRealId");
		/**取树的数据*/
		TreeChainData tcd = new TreeChainData(getData(tableName, nodeId, levelDown),
											  mUser.getUserID(),
				                              nodeId,
				                              this.fieldMap,
				                              TOOLS.stringToInt(levelDown));
		tcd.setData(nodeLevel, haveBrother, haveLBrother, parentsHaveLBrother);
		tcd.setVirtualRootNode(VirtualRootName, null);
		tcd.create();
		tcd.setTableName(tableName);
		tcd.setLevelDown(TOOLS.stringToInt(levelDown));
		return tcd;
	}

	@Override
	public String getTableName() throws ErrorMessage {
		return tableName;
	}

	@Override
	public Tree_DC_Type getType() throws ErrorMessage {
		return Tree_DC_Type.TreeParent;
	}
	
	@Override
	public String getOperatorId() throws ErrorMessage {
		return null;
	}

	@Override
	public String getRowId() throws ErrorMessage {
		return null;
	}
	
	/**
	 * 取父子树的树字段映射参数,顺序如下
	 *   NODE_ID,NODE_NAME,NODE_PARENT,NODE_TYPE,OPERATOR_TYPE,NODE_CODE,NODE_LEVEL
	 * @return String 字段映射字符串
	 * @throws ErrorMessage
	 */
	@Override
	public String getField() throws ErrorMessage {
		StringBuilder sb = new StringBuilder();
		sb.append(this.fieldMap.getNodeId());
		sb.append(">");
		sb.append(this.fieldMap.getNodeName());
		sb.append(">");
		sb.append(this.fieldMap.getNodeParent());
		sb.append(">");
		sb.append(this.fieldMap.getNodeType());
		sb.append(">");
		sb.append(this.fieldMap.getOperatorType());
		sb.append(">");
		sb.append(this.fieldMap.getRealId());
		sb.append(">");
		sb.append(this.fieldMap.getNodeLevel());
		
		return sb.toString();
	}

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		return null;
	}
	
	/**
	 * 拼接父子树的查询sql
	 * @param tableName 表名
	 * @param rootValue 根节点Id
	 * @param levelDown 向下展开几级
	 * @param filterSql 过滤条件
	 * @param nodeIdMapping 映射字段-节点id
	 * @param nodeParentIdMapping 映射字段-父节点id
	 * @return 父子树的查询sql
	 * @throws ErrorMessage
	 */
	private SqlResult getData(String tableName,
						  String rootValue,
						  String levelDown) throws ErrorMessage{
		TreeParentDataMap fieldMap = new TreeParentDataMap();
		fieldMap.setNodeId(this.fieldMap.getNodeId());
		fieldMap.setNodeParent(this.fieldMap.getNodeParent());
		fieldMap.setNodeName(this.fieldMap.getNodeName());
		fieldMap.setRealId(this.fieldMap.getRealId());
		fieldMap.setOperatorType(this.fieldMap.getOperatorType());
		TreeParentData sql = TreeParentData.getNewInstance(new DbResource(),tableName,fieldMap,rootValue);
		sql.start();
		SqlResult al = sql.getReslut();
		return al;
	}

}
